<template>
    <div>
        <el-tabs type="border-card" v-model="query.showtype"  @tab-click="change">
            <el-tab-pane label="全部" :name="0">
                <div class="container">
                    <el-table
                        :data="tableData"
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                        @selection-change="handleSelectionChange"
                    >   
                        <el-table-column  prop="username" label="账号"></el-table-column>
                        <el-table-column  prop="nickname" label="昵称"></el-table-column>
                        <el-table-column  prop="wtnum" label="委托编号"></el-table-column>
                        <el-table-column prop="buynum" label="委托数量"></el-table-column>
                        <el-table-column  prop="contract" label="合约编号"></el-table-column>
                        <el-table-column  prop="codename" label="合约名称"></el-table-column>
                        
                        <el-table-column  prop="cmd" label="方向">
                            <template #default="{ row }">
                                <div>
                                   {{ row.risefall == 'rise' ? '买':'卖' }}
                                </div>
                            </template>
                        </el-table-column>

                        <el-table-column  prop="volume" label="下单数量"></el-table-column>
                        <el-table-column  prop="buynum" label="数量"></el-table-column> 
                        <el-table-column  prop="playprice" label="持仓价"></el-table-column>
                        <el-table-column prop="yk" label="持仓盈亏"></el-table-column>
                        <el-table-column  prop="stop_profit_price" label="止盈"></el-table-column>
                        <el-table-column  prop="stop_loss_price" label="止损"></el-table-column>
                        <el-table-column  prop="margin" label="保证金"></el-table-column>
                        <el-table-column  prop="fee" label="手续费"></el-table-column>
                        <el-table-column  prop="rate" label="币种"></el-table-column>
                        <el-table-column  prop="rate" label="成交时间">
                            <template #default="{ row }">
                                <div>
                                    {{ dayjs.unix(row.updatetime).format('YYYY-MM-DD HH:mm:ss') }}
                                </div>
                                </template>
                        </el-table-column> 

                    </el-table>
              
                </div>
            </el-tab-pane>
            <el-tab-pane label="可撤" :name="1">
                <el-table
                        :data="tableData"
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                        @selection-change="handleSelectionChange"
                    >   
                        <el-table-column  prop="username" label="账号"></el-table-column>
                        <el-table-column  prop="nickname" label="昵称"></el-table-column>
                        <el-table-column  prop="wtnum" label="委托编号"></el-table-column>
                        <el-table-column  prop="volume" label="下单数"></el-table-column>
                        <el-table-column  prop="contract" label="合约编号"></el-table-column>
                        <el-table-column  prop="codename" label="合约名称"></el-table-column>
                        
                        <el-table-column  prop="cmd" label="方向">
                            <template #default="{ row }">
                                <div>
                                   {{ row.risefall == 'rise' ? '买':'卖' }}
                                </div>
                            </template>
                        </el-table-column>

                        <el-table-column  prop="volume" label="下单数量"></el-table-column>
                        <el-table-column  prop="buynum" label="数量"></el-table-column> 
                        <el-table-column  prop="playprice" label="持仓价"></el-table-column>
                        <el-table-column prop="yk" label="持仓盈亏"></el-table-column>
                        <el-table-column  prop="stop_profit_price" label="止盈"></el-table-column>
                        <el-table-column  prop="stop_loss_price" label="止损"></el-table-column>
                        <el-table-column  prop="margin" label="保证金"></el-table-column>
                        <el-table-column  prop="fee" label="手续费"></el-table-column>
                        <el-table-column  prop="rate" label="币种"></el-table-column>
                        <el-table-column  prop="rate" label="成交时间">
                            <template #default="{ row }">
                                <div>
                                    {{ dayjs.unix(row.updatetime).format('YYYY-MM-DD HH:mm:ss') }}
                                </div>
                                </template>
                        </el-table-column>

                        <el-table-column
                            show-overflow-tooltip
                            fixed="right"
                            align="center"
                            label="操作"
                            width="70"
                        >
                            <template #default="{ row }">
                            <el-button type="text" @click="chedan(row)">
                                撤单
                            </el-button>
                            </template>
                        </el-table-column>

                    </el-table>
            </el-tab-pane>
        </el-tabs>

        
        

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { weituo,chedanapi } from "../api/index";
import dayjs from 'dayjs'
export default {
    name: "basetable",
    data() {
        return {
            dayjs:dayjs,
            query: {
                showtype: 0
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        //this.getData();
        this.timer = setInterval(() => {
            this.getData();
        }, 10000);
        this.getData();
    },
    methods: {

        chedan(row) {
            this.$confirm('确定强制撤单吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                }).then(() => {
                chedanapi({wtnum: row.wtnum}).then(res => {
                    this.$message.success(res.msg)
                    this.getData()
                }).catch(ret => {
                    this.$message.error(ret.msg)
                })
            })
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            weituo(this.query).then(res => {
                this.tableData = res.data.list;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, "pageIndex", 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index) {
            // 二次确认删除
            this.$confirm("确定要删除吗？", "提示", {
                type: "warning"
            })
                .then(() => {
                    this.$message.success("删除成功");
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        change() {
           // this.collapse = false
            //this.selectItem = null
            this.getData()
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = "";
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + " ";
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, "pageIndex", val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.el-table td, .el-table th.is-leaf {
    border-bottom: 0;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
